package com.qqtop10.manager.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qqtop10.manager.dao.CategoryMapper;
import com.qqtop10.manager.model.Category;
import com.qqtop10.manager.model.PagingArgs;
import com.qqtop10.manager.model.temp.BuildIndexCategoryTemp;
import com.qqtop10.manager.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl extends BaseService implements CategoryService {
	@Autowired
	private CategoryMapper categoryMapper;

	@Override
	public List<Category> all(Category input, PagingArgs pagingArgs) {
		return categoryMapper.all(asMap(input, pagingArgs));
	}

	@Override
	public int count(Category input) {
		try {
			return categoryMapper.count(input);
		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public boolean update(Category input) {
		return categoryMapper.update(input) != 0;
	}

	@Override
	public boolean add(Category input) {
		return categoryMapper.add(input) != 0;
	}

	@Override
	public boolean delete(Category input) {
		Category flag = new Category();
		flag.setParentId(input.getId());
		if (count(flag) != 0) {
			throw new RuntimeException("不能删除存在子级的节点。");
		}
		return categoryMapper.delete(input) != 0;
	}

	@Override
	public Category get(Category input) {
		return categoryMapper.get(input);
	}

	@Override
	public List<Category> selectAll() {
		return categoryMapper.selectAll();
	}

	@Override
	public List<Category> selectByIntCategoryUuid(String intCategoryUuid) {
		return categoryMapper.selectByIntCategoryUuid(intCategoryUuid);
	}

	/**
	 * 查找为删除的一级分类
	 */
	@Override
	public List<Category> selectOneCategory() {
		return categoryMapper.selectOneCategory();
	}

	/**
	 * 
	 * @param map[icon:一级分类图标,
	 *            id:一级分类ID]
	 * @return
	 */
	@Override
	public int updateCategoryIcon(Map<String, Object> map) {
		return categoryMapper.updateCategoryIcon(map);
	}

	@Override
	public Category selectByUuid(String uuid) {
		return categoryMapper.selectByUuid(uuid);
	}

	@Override
	public List<BuildIndexCategoryTemp> buildIndexGetCategory() {
		List<BuildIndexCategoryTemp> categoryList = categoryMapper.buildIndexGetCategory();
		return categoryList;
	}

}
